package SnailCatMallWebAppliction.Utils;

import JAVAFXManyWindow.pojo.MainMe;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.*;

public class DataConnection {
    public SqlSession getSqlsession()throws IOException{
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        byte[] dat = new byte[inputStream.available()];
        inputStream.read(dat);
        inputStream.close();
        String basv = new String(dat,"UTF-8");
        basv = basv.replace("${jdbc.url}","jdbc:h2:file:"+ MainMe.jxBrowserServerTcp.getURLAddress() +"DataBaseSystem");
        basv=basv.replace("${jdbc.driverClassName}","org.h2.Driver");
        basv=basv.replace("${jdbc.username}","root");
        basv=basv.replace("${jdbc.password}","root123456");
        InputStream is = new ByteArrayInputStream(basv.getBytes("UTF-8"));
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        is.close();
        return sqlSession;
    }
}
